//包装uniapp图片/文件相关api
import {getFileAccessHttpUrl, uploadAction} from "@/util/api";

export class FileUtils{
    static imgSuffix = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']
    //判断是否是图片
    static isImg(fileName) {
        let suffix = fileName.split('.')[1]
        return this.imgSuffix.includes(suffix)
    }
    //获取文件后缀
    static getSuffix(fileName) {
        return fileName.split('.')[1]
    }
    /**
     * 选择图片.默认选择1张,选择一张时直接返回该张的图片地址(本地)
     * @param imgNumber
     * @param compress
     * @param config
     * @returns {Promise<*>}
     */
    static async chooseImage(imgNumber = 1,compress=true, config = {}) {
        let param = {
            count: imgNumber, //默认
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            // sourceType: ['album'], //从相册选择
        }
        let [err, res] = await uni.chooseImage({...param, ...config})
        if (err) {
            return Promise.reject(err)
        }
        let {tempFilePaths} = res
        //H5浏览器不支持压缩
        /* #ifdef H5 */
        compress=false
        /* #endif */
        if (compress){
            let result2 = await Promise.all(tempFilePaths.map(async (item) => {
                return await this.compressImage(item)
            }))
            return result2
        }
        return tempFilePaths
    }

    /**
     * 上传图片 完整信息
     * @param imgNumber
     * @param compress
     * @param config
     * @returns {Promise<Awaited<unknown>[]|*>}
     */
    static async chooseImageFull(imgNumber = 1,compress=true, config = {}) {
        let param = {
            count: imgNumber, //默认
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            // sourceType: ['album'], //从相册选择
        }
        let [err, res] = await uni.chooseImage({...param, ...config})
        if (err) {
            return Promise.reject(err)
        }
        console.log("图片上传结果",res)
        let {tempFilePaths} = res
        //H5浏览器不支持压缩
        /* #ifdef H5 */
        compress=false
        /* #endif */
        if (compress){
            let result2 = await Promise.all(tempFilePaths.map(async (item) => {
                return await this.compressImage(item)
            }))
            return res.tempFilePaths = result2
        }
        return res
    }
    /**
     * 选择图片并上传
     * @param imgNumber
     * @param compress
     * @param config
     */
    static async chooseAndUpload(imgNumber = 1,compress=true, config = {}){
        let fileArray = await this.chooseImage(imgNumber,compress,config)
        //同时上传图片数组
        let result = await Promise.all(fileArray.map(async (item) => {
            let result = await uploadAction(item)
            return getFileAccessHttpUrl(result)
        }))
        console.log("fileArray",result)
        return result
    }

    /**
     * 压缩图片
     * @param src 图片路径
     * @param quality 压缩质量
     * @param config 配置
     * @returns {Promise<*>} 新的图片路径
     */
    static async compressImage(src, quality=0.8,config = {}) {
        try {
            //如果是H5,不支持压缩
            /* #ifdef H5 */
            return src
            /* #endif */

            let [err, res] = await uni.compressImage({src, quality, ...config})
            console.log("压缩后",res.tempFilePath)
            return res.tempFilePath
        } catch (e) {
            console.log(e)
            return Promise.reject(e)
        }
    }
}
